package com.ttg.web.dao.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ttg.common.entities.BasePage;
import com.ttg.common.entities.PagesRes;
import com.ttg.common.enums.TagTypeEnum;
import com.ttg.model.dto.*;
import com.ttg.model.mapper.TagInfoMapper;
import com.ttg.model.pojo.TagInfo;
import com.ttg.model.vo.*;
import com.ttg.web.dao.TagInfoDao;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author ZQ
 * @version 1.0
 * @Description
 * @date 2021/1/26 13:26
 */
@Service
public class TagInfoDaoImpl extends ServiceImpl<TagInfoMapper, TagInfo> implements TagInfoDao {


    @Override
    public BasePage<TagVo> tagPage(TagPageDTO reqDto) {
        return baseMapper.tagPage(reqDto.parse(),reqDto);
    }
    @Override
    public Set<String> getAgents(TagVipPageDTO reqDto) {
        Long tagId = reqDto.getTagId();
        List<TagVipPageAgentDto> dtos=baseMapper.getAgents(tagId);
        StringBuffer sb=new StringBuffer();
        String agentsStr=null;
        Set<String> agents=new HashSet();
        for (int i = 0; i < dtos.size(); i++) {
            TagVipPageAgentDto dto = dtos.get(i);
            if (dto.getConditionsId()==108){
                sb.append(dto.getData1());
                break;
            }
            sb.append(dto.getData1());
            if (i!=dtos.size()-1){
                sb.append(";");
            }
        }
        agentsStr=sb.toString();
        if (agentsStr.contains(";")){
            String[] strings = agentsStr.split(";");
            for (String str : strings) {
                agents.add(str);
            }
        }else {
            if (!agentsStr.isEmpty()){
                agents.add(agentsStr);
            }
        }
        return agents;
    }

    @Override
    public List<VipTag> getTagListByClass(Integer vipId, Integer tagClassId) {
        return baseMapper.getTagListByClass( vipId,  tagClassId);
    }

    @Override
    public BasePage<TagVipVo> tagVipPage(TagVipPageDTO reqDto, Set<String> agents, Boolean countFlag) {
        BasePage<TagVipVo> tagVipVoBasePage = new BasePage<>();

        Long count = null;
        if (countFlag) {
            if ((Objects.nonNull(reqDto.getVipName()) && !reqDto.getVipName().isEmpty()) ||
                    (Objects.nonNull(reqDto.getMobile()) && !reqDto.getMobile().isEmpty()) ||
                    (Objects.nonNull(reqDto.getVipId()))
            ) {
                count = baseMapper.tagVipPageCount(reqDto.getTagId(), reqDto.getVipId(), reqDto.getVipName(), reqDto.getMobile());
            } else {
                TagInfo one = getOne(new LambdaQueryWrapper<TagInfo>().select(TagInfo::getTagPeopleNumber).eq(TagInfo::getId, reqDto.getTagId()));
                count = one.getTagPeopleNumber().longValue();
            }
            tagVipVoBasePage.setTotal(count);
        }
        List<TagVipVo> tagVipVos = null;
        if (count==null||count > 0) {
            tagVipVos = baseMapper.tagVipPage((reqDto.getPage() - 1) * reqDto.getPageSize(), reqDto.getPageSize(), reqDto.getTagId(), reqDto.getVipId(), reqDto.getVipName(), reqDto.getMobile(), agents);
        } else {
            tagVipVos = new ArrayList<>();
        }
        tagVipVoBasePage.setList(tagVipVos);
        tagVipVoBasePage.setSize(reqDto.getPageSize());
        PagesRes pagesRes = tagVipVoBasePage.getPagesRes();
        pagesRes.setPage(reqDto.getPage());
        pagesRes.setPagesize(reqDto.getPageSize());
        List<TagVipVo> list = tagVipVoBasePage.getList();
        for (TagVipVo tagVipVo : list) {
            String mobile = tagVipVo.getMobile();
            if (mobile != null) {
                if (mobile.length() <= 10) {
                    tagVipVo.setMobile(mobile.replaceAll("(\\d{0})\\d{5}(\\w{2})", "$1*****$2"));
                } else {
                    tagVipVo.setMobile(mobile.replaceAll("(\\d{3})\\d{4}(\\w{4})", "$1****$2"));
                }
            }
        }
        return tagVipVoBasePage;
    }

    @Override
    public BasePage<TagVo> tagPageByVip(TagVipPageDTO reqDto) {
        return baseMapper.tagPageByVip(reqDto.parse(),reqDto.getVipId());
    }

    @Override
    public BasePage<TagMctVo> tagPageByMct(TagMctPageDTO reqDto) {
        return baseMapper.tagPageByMct(reqDto.parse(),reqDto.getMctNo().toString());
    }

    @Override
    public BasePage<MctTagPageVo> mctTagPage(MctTagPageDto reqDto) {
        return baseMapper.mctTagPage(reqDto.parse(), reqDto.getMctId(), reqDto.getMctName(), reqDto.getTagId());
    }


    @Override
    public List<TagInfo> getAllByTypeAndStatus(TagTypeEnum tagTypeEnum) {
        LambdaQueryWrapper<TagInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TagInfo::getTagType,tagTypeEnum.getValue());
        lambdaQueryWrapper.eq(TagInfo::getTagStatus,0);
        return list(lambdaQueryWrapper);
    }

    @Override
    public BasePage<GetVipTagListVo> getVipTagList(BasePage<GetVipTagListVo> basePage, GetVipTagListDto dto) {

        return baseMapper.getVipTagList(basePage,dto);
    }

    @Override
    public BasePage<TagInfo> getAllByTypeAndStatusPage(BasePage basePage,TagTypeEnum type) {
        LambdaQueryWrapper<TagInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TagInfo::getTagType,type.getValue());
        lambdaQueryWrapper.eq(TagInfo::getTagStatus,0);
        return page(basePage,lambdaQueryWrapper);
    }

    @Override
    public List<TagInfo> queryBindGradeTags(Integer gradeId) {
        LambdaQueryWrapper<TagInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(TagInfo::getId,TagInfo::getTagName,TagInfo::getTagPeopleNumber,TagInfo::getAddId)
                .eq(TagInfo::getVipGradeId,gradeId);
        return list(wrapper);
    }

    @Override
    public BasePage<MemberConfigTagsForBindVo> queryTagsForBind(BasePage<MemberConfigTagsForBindVo> pageSetting, Integer gradeId, Integer tagType) {
        return baseMapper.queryTagsForBind(pageSetting,gradeId,tagType);
    }

    @Override
    public void cancelBindGradeByGradeId(Integer id) {
        LambdaUpdateWrapper<TagInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(TagInfo::getVipGradeId,id)
                .set(TagInfo::getVipGradeId,null);
        update(wrapper);
    }

    @Override
    public void bindGradeId(Integer gradeId, List<Long> tagIds,Integer type) {
        LambdaUpdateWrapper<TagInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(TagInfo::getId,tagIds)
                .eq(TagInfo::getTagType,type)
                .set(TagInfo::getVipGradeId,gradeId);
        update(wrapper);
    }

    @Override
    public List<TagInfo> selectByIds(List<Integer> containsTagIds) {
        LambdaQueryWrapper<TagInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(TagInfo::getId, containsTagIds);
        wrapper.eq(TagInfo::getTagStatus, 0);
        return list(wrapper);
    }
}
